package com.imay.live.activity.family.helper;

import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.imay.live.activity.family.FamilyHomeActivity;
import com.imay.live.activity.family.controller.FamilyHomeController;
import com.netease.nim.uikit.LoginSyncDataStatusObserver;
import com.netease.nim.uikit.NimUIKit;
import com.netease.nim.uikit.common.ui.dialog.DialogMaker;
import com.netease.nim.uikit.common.ui.drop.DropManager;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.common.util.string.MD5;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;

/**
 * Created by Administrator on 2017/8/29 0029.
 */

public class NimLogin {

    private static NimLogin instance;

    private AbortableFuture<LoginInfo> loginRequest;

    private final static int LOGIN_STATE_IDE = 0;
    private final static int LOGIN_STATE_ING = 1;
    private final static int LOGIN_STATE_SUCCESS = 2;
    private final static int LOGIN_STATE_ERROR = 3;
    private final static int LOGIN_STATE_KICKOUT = 4;

    private int nimLoginStatus = LOGIN_STATE_IDE;

    public static final String account = "15975641877";
    public static final String psd = "123456";

    public static NimLogin getInstance() {
        if (instance == null) {
            instance = new NimLogin();
        }
        return instance;
    }

    public void loginNim(final Context context, String account, String psd) {
        setNimLoginStatus(LOGIN_STATE_ING);
//        DialogMaker.showProgressDialog(context, null, "登录中", true, new DialogInterface.OnCancelListener() {
//            @Override
//            public void onCancel(DialogInterface dialog) {
//                if (loginRequest != null) {
//                    loginRequest.abort();
//                    onLoginDone();
//                }
//            }
//        }).setCanceledOnTouchOutside(false);

        final String token = tokenFromPassword(context, psd);

        // 登录
        loginRequest = NimUIKit.doLogin(new LoginInfo(account, token), new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo param) {
                LogUtil.i("ttt", "login success");
                setNimLoginStatus(LOGIN_STATE_SUCCESS);
                onLoginDone();
            }

            @Override
            public void onFailed(int code) {
                LogUtil.i("ttt", "login onFailed");
                setNimLoginStatus(LOGIN_STATE_ERROR);
                onLoginDone();
            }

            @Override
            public void onException(Throwable exception) {
                LogUtil.i("ttt", "login exception");
                setNimLoginStatus(LOGIN_STATE_ERROR);
                onLoginDone();
            }
        });
    }

    private void onLoginDone() {
        loginRequest = null;
        //DialogMaker.dismissProgressDialog();
    }

    public void logout() {
        // 清理缓存&注销监听&清除状态
        setNimLoginStatus(LOGIN_STATE_IDE);
        NimUIKit.clearCache();
        ChatRoomHelper.logout();
        LoginSyncDataStatusObserver.getInstance().reset();
        DropManager.getInstance().destroy();
    }

    public void setNimLoginStatus(int nimLoginStatus) {
        this.nimLoginStatus = nimLoginStatus;
    }

    public boolean isLoginNim() {
        return nimLoginStatus == LOGIN_STATE_SUCCESS;
    }

    //DEMO中使用 username 作为 NIM 的account ，md5(password) 作为 token
    //开发者需要根据自己的实际情况配置自身用户系统和 NIM 用户系统的关系
    private String tokenFromPassword(Context context, String password) {
        String appKey = readAppKey(context);
        boolean isDemo = "45c6af3c98409b18a84451215d0bdd6e".equals(appKey)
                || "fe416640c8e8a72734219e1847ad2547".equals(appKey);

        return isDemo ? MD5.getStringMD5(password) : password;
    }

    private static String readAppKey(Context context) {
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (appInfo != null) {
                return appInfo.metaData.getString("com.netease.nim.appKey");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
